<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;

class Article extends Base {

    public function index(){
        //$map = $this->_search();
        $clist = Db::name('article_cat')->where('1=1')->select();
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('cat_id')){
                $map['a.cat_id'] = input('cat_id');
            }
            $count = Db::name('article')->alias('a')->where($map)->count();

            $field = 'a.*,c.cat_name';
            $list = Db::name('article')
                ->alias('a')
                ->join('article_cat c','a.cat_id=c.cat_id')
                ->field($field)
                ->where($map)
                ->order('a.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        View::assign('clist',$clist);
        return View::fetch();
    }
    public function edit(){
        $id = input('id');
        $info = Db::name('article')->where('id',$id)->find();
        $clist = Db::name('article_cat')->where('1=1')->select();
        View::assign('info',$info);
        View::assign('clist',$clist);
        return View::fetch();
    }
    public function save_article(){
        $params = input();
        $id = input('id')?:'';
        $data = [];
        $data['title'] = $params['title'];
        $data['cat_id'] = $params['cat_id'];
        $data['description'] = $params['description'];
        $data['thumb'] = $params['thumb'];
        $data['content'] = htmlspecialchars_decode($params['content']);
        $data['add_time'] = time();
        if($id){
            Db::name('article')->where('id',$id)->update($data);
        }else{
            Db::name('article')->insert($data);
        }
        return json(['status'=>true,'msg'=>'操作成功']);
    }
    public function del(){
        $params = input();
        $ids = input('ids/a')?:'';
        Db::name('article')->whereIn('id',$ids)->delete();

        return json(['status'=>true,'msg'=>'操作成功']);
    }
    public function categoryList(){
        $clist = Db::name('article_cat')->where('1=1')->column('cat_name','cat_id');
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('parent_id')){
                $map['a.parent_id'] = input('parent_id');
            }
            if(input('cat_name')){
                $map['a.cat_name'] = input('cat_name');
            }
            $count = Db::name('article_cat')->alias('a')->where($map)->count();

            $field = '*';
            $list = Db::name('article_cat')
                ->alias('a')
                ->field($field)
                ->where($map)
                ->order('cat_id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['parent_name'] = $clist[$v['parent_id']]??'';
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        View::assign('clist',$clist);
        return View::fetch();
    }

    public function edit_category()
    {
        $params = input();
        $id = input('id')?:'';
        if(request()->isPost()){
            $data = [];
            $data['cat_name'] = $params['cat_name'];
            $data['parent_id'] = $params['parent_id'];
            $data['sort_order'] = $params['sort_order'];
            $data['w_time'] = time();
            if($id){
                Db::name('article_cat')->where('cat_id',$id)->update($data);
            }else{
                Db::name('article_cat')->insert($data);
            }
            return json(['status'=>true,'msg'=>'操作成功']);
        }else{
            if($id){
                $info = Db::name('article_cat')->where('cat_id',$id)->find();
            }else{
                $info = [];
            }
            View::assign('info',$info);

            $clist = Db::name('article_cat')->where('parent_id',0)->select();
            View::assign('clist',$clist);
            return View::fetch();
        }

    }
    public function del_cat(){
        $params = input();
        $ids = input('ids/a')?:'';
        Db::name('article_cat')->whereIn('cat_id',$ids)->delete();

        return json(['status'=>true,'msg'=>'操作成功']);
    }
}